﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Babelfish
{
    /// <summary>
    /// 
    /// </summary>
    public class AttributeCollection:IEnumerable<IAttribute>
    {
        private readonly List<IAttribute> _list = new List<IAttribute>();

        /// <summary>
        /// 
        /// </summary>
        public delegate void CollectionUpdatedHandler(object sender, EventArgs args);

        /// <summary>
        /// Occurs when [collection updated].
        /// </summary>
        public event CollectionUpdatedHandler CollectionUpdated;

        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified key.
        /// </summary>
        /// <value></value>
        public string this[string key]
        {
            get
            {
                var attribute = this._list.Where(a => a.Name.ToLower() == key.ToLower()).FirstOrDefault();
                return attribute == null ? null : attribute.Value;
            }
            set
            {
                var attribute = this._list.Where(a => a.Name.ToLower() == key.ToLower()).FirstOrDefault();
                if (attribute == null)
                    return;

                attribute.Value = value;
                CollectionUpdated(this, new EventArgs());
            }
        }

        /// <summary>
        /// Adds the specified attribute.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        public void Add(IAttribute attribute)
        {
            attribute.ParentCollection = this;
            _list.Add(attribute);   
            if(CollectionUpdated != null)
                CollectionUpdated(this, new EventArgs());
        }

        /// <summary>
        /// Adds the range.
        /// </summary>
        /// <param name="list">The list.</param>
        public void AddRange(IEnumerable<IAttribute> list)
        {
            foreach (var attribute in list)
                attribute.ParentCollection = this;
            
            _list.AddRange(list);

            if(CollectionUpdated != null)
                CollectionUpdated(this, new EventArgs());
        }

        /// <summary>
        /// Removes the specified attribute.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        public void Remove(IAttribute attribute)
        {
            _list.Remove(attribute);

            if(CollectionUpdated != null)
                CollectionUpdated(this, new EventArgs());
        }

        /// <summary>
        /// Removes the specified attributes.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public void Remove(IEnumerable<IAttribute> attributes)
        {
            foreach (var attribute in attributes)
                _list.Remove(attribute);

            if(CollectionUpdated != null)
                CollectionUpdated(this, new EventArgs());
        }

        /// <summary>
        /// Determines whether [contains] [the specified key].
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        /// 	<c>true</c> if [contains] [the specified key]; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string key)
        {
            return this._list.Where(a => a.Name == key).Count() > 0;
        }

        public IEnumerator<IAttribute> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
